package com.ai.platform.core.context;


import com.ai.platform.model.dto.Input;
import com.ai.platform.model.dto.Output;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yeyingsheng on 2018/5/1.
 * 上下文请求对象持有者
 */
public class InvocationContextHolder {

    private static final Logger logger = LoggerFactory.getLogger(InvocationContextHolder.class);

    private static ThreadLocal<InvocationContextHolder> contexts = new ThreadLocal<InvocationContextHolder>();

    private ServletContext servletContext;
    private HttpSession session;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Map<String, Cookie> cookies;
    private Input input=new Input();
    private Output output=new Output();
    private InvocationContextHolder(){}

    public static void setContext(ServletContext servletContext, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        InvocationContextHolder rc = new InvocationContextHolder();
        rc.servletContext = servletContext;
        rc.request = httpServletRequest;
        rc.response = httpServletResponse;
        rc.session = httpServletRequest.getSession();
        rc.cookies = new HashMap<String, Cookie>();
        Cookie[] cookies = httpServletRequest.getCookies();
        if(cookies != null){
            for(Cookie ck : cookies) {
                rc.cookies.put(ck.getName(), ck);
            }
        }
        contexts.set(rc);
    }

    public static InvocationContextHolder getContext(){
        InvocationContextHolder ci = contexts.get();
        if (ci == null) {
            logger.debug("设置上下文对象");
            ci = new InvocationContextHolder();
            // 初始化参数
            contexts.set(ci);
        }
        return ci;
    }

    public static void clearContext() {
        logger.debug("清除上下文对象");
        contexts.remove();
    }

    public ServletContext getServletContext() {
        return servletContext;
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public Map<String, Cookie> getCookies() {
        return cookies;
    }

    public void setCookies(Map<String, Cookie> cookies) {
        this.cookies = cookies;
    }

    public Input getInput() {
        return input;
    }

    public void setInput(Input input) {
        this.input = input;
    }

    public Output getOutput() {
        return output;
    }

    public void setOutput(Output output) {
        this.output = output;
    }
}
